package com.xyw.code.authentication.server.events;

import com.xyw.code.authentication.server.config.BusConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Base64UrlNamingStrategy;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * User: xuyiwei
 * Date: 2020/2/17
 * Time: 下午9:11
 * Email: 1328312923@qq.com
 * Description: No Description
 **/
@Component
@Slf4j
public class BusReceiver {

    public static String appName;

    @Bean
    public org.springframework.amqp.core.Queue queue(){
        String queneName = new Base64UrlNamingStrategy(appName+".").generateName();
        log.info("队列名称:{}",queneName);
        return new org.springframework.amqp.core.Queue(queneName,false);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "#{queue.name}", durable = "false"),
            exchange = @Exchange(
                    value = BusConfig.EXCHANGE_NAME,
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {BusConfig.ROUTING_KEY}
    ))
    @RabbitHandler
    public void process(String message) {
        log.info("Received Message:<{}>", message);

    }

    //直接用的@value  也可以通过 environment等去获取
    @Value("${spring.application.name}")
    public void setAppName(String appName) {
        this.appName = appName;
    }
}
